home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / packer / zoo / basename.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  3KB  |  126 lines

  1. #ifndef LINT
  2. /* @(#) basename.c 2.2 87/12/27 13:42:40 */
  3. static char sccsid[]="@(#) basename.c 2.2 87/12/27 13:42:40";
  4. #endif /* LINT */
  5.  
  6. /*
  7. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  8. */
  9.  
  10. #include "options.h"
  11. #include "zooio.h"
  12. #include "zoo.h"
  13. #include "parse.h"
  14. #include "various.h"
  15. #include "zoofns.h"
  16. #include "debug.h"
  17. #include "assert.h"
  18.  
  19. void cleanup PARMS((char *));
  20.  
  21. /* This function strips device/directory information from
  22. a pathname and returns just the plain filename */
  23. void basename (pathname, fname)
  24. char *pathname;
  25. char fname[];
  26. {
  27.     strcpy (fname, nameptr (pathname));
  28. }
  29.  
  30. /* Set of legal MSDOS filename characters.  The working of cvtchr() depends
  31. on the order of the first few characters here.    In particular, '_' is
  32. positioned so '.' gets converted to it. */
  33. static char legal[] =
  34. "tabcdefghijklmnopqrs_uvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@^`{}~!#$%&'()-";
  35.  
  36. /****************
  37. cvtchr() converts a character to a lowercase alphabetic character in
  38. a somewhat random way.
  39. */
  40. #define    cvtchr(ch)        legal[(ch & 0xff) % 26]
  41.  
  42. /***************
  43. cleanup() cleans up a string so it contains only legal MSDOS filename
  44. characters.  Any other characters are converted to an underscore.
  45. If the filename is null or if it begins with a dot, it is fixed.
  46. All dots are also converted.
  47. */
  48.  
  49. void cleanup (p)
  50. char *p;
  51. {
  52.     assert(p != NULL);
  53.     if (*p == '\0')
  54.         strcpy (p, "X");
  55.     if (*p == '.')
  56.         *p = '_';
  57.     while (*p != '\0') {
  58.         if (strchr (legal, *p) == NULL) {   /* if invalid character */
  59.             *p = cvtchr(*p);
  60.         }
  61.         p++;
  62.     }
  63. }
  64. /* This function strips device/directory information from a pathname,
  65. forces the remaining filename to MSDOS format, and returns it.  Any
  66. illegal characters are fixed.
  67. */
  68. void dosname (pathname, fname)
  69. char *pathname;
  70. char fname[];
  71. {
  72.     struct path_st path_st;
  73.     parse (&path_st, pathname);
  74.     strcpy (fname, path_st.fname);
  75.     cleanup (fname);
  76.  
  77. #ifdef VER_CH    /* remove any trailing extension field */
  78.     if (path_st.ext[0] != '\0')
  79.         strip_ver (path_st.ext);
  80. #endif
  81.  
  82.     /* extension could have been nulled, so we test again */
  83.     if (path_st.ext[0] != '\0') {
  84.         cleanup (path_st.ext);
  85.         strcat (fname, ".");
  86.         strcat (fname, path_st.ext);
  87.     }
  88.  
  89. #ifdef SPECMOD
  90.     specfname (fname);
  91. #endif
  92. }
  93.  
  94. /* rootname() */
  95. /* Accepts a pathname.    Returns the root filename, i.e., with both the
  96. directory path and the extension stripped. */
  97.  
  98. void rootname (path, root)
  99. char *path, *root;
  100. {
  101.     char *p;
  102.     static char dot[] = {EXT_CH, '\0'};
  103.     strcpy(root, nameptr(path));           /* copy all but path prefix */
  104.     p = findlast(root, dot);               /* find last dot */
  105.     if (p != NULL)                         /* if found ... */
  106.         *p = '\0';                          /* ... null it out */
  107. }
  108.  
  109. /* nameptr() */
  110. /* Accepts a pathname.    Returns a pointer to the filename within
  111. that pathname.
  112. */
  113.  
  114. char *nameptr (path)
  115. char *path;
  116. {
  117.     char *t;
  118.     t = findlast (path, PATH_SEP);   /* last char separating device/directory */
  119.     debug ((printf ("nameptr:  findlast returned ptr to string [%s].\n",t)))
  120.     if (t == NULL)                /* no separator */
  121.         return (path);
  122.     else {
  123.         return (t+1);
  124.     }
  125. }
  126.